﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
namespace BaHuan
{
    public partial class FrmTimKiemPhieuChiTien : Form
    {
        private PhieuChiTienManagement phieuchitienMgt = new PhieuChiTienManagement();
        private UyNhiemChiManagement uynhiemchiMgt = new UyNhiemChiManagement();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmTimKiemPhieuChiTien()
        {
            InitializeComponent();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;

            List<PhieuChiTien> lstPhieuChiTien = (from pct in GetAllPhieuChiTien()
                                                  where (pct.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                  pct.ChungTuKemTheo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                  pct.LyDo.Contains(txtTimKiem.Text)) &&
                                                  pct.NgayThang.Date >= dtTuNgay.Date && pct.NgayThang.Date <= dtDenNgay.Date &&
                                                  pct.IsDelete==0
                                                  select pct).ToList();

            dgvDanhSachPhieuChiTien.AutoGenerateColumns = false;
            dgvDanhSachPhieuChiTien.DataSource = lstPhieuChiTien;
        }

        private void dgvDanhSachPhieuChiTien_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            PhieuChiTien phieuchitien = (PhieuChiTien)dgvDanhSachPhieuChiTien.CurrentRow.DataBoundItem;
            if(phieuchitien != null)
            {
                FrmPhieuChiTien frmPhieuChiTien = new FrmPhieuChiTien(phieuchitien, nhanvien);
                this.Hide();
                frmPhieuChiTien.ShowDialog();
                this.Show();
                btnTimKiem_Click(null, null);
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            FrmPhieuChiTien frmPhieuChiTien = new FrmPhieuChiTien(this.nhanvien);
            this.Hide();
            frmPhieuChiTien.ShowDialog();
            this.Show();
        }

        public List<PhieuChiTien> GetAllPhieuChiTien()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = phieuchitienMgt.GetAllPhieuChiTien();
            List<PhieuChiTien> listPhieuChiTien = (List<PhieuChiTien>)resultObj.Result;
            return listPhieuChiTien;
        }

        public List<UyNhiemChi> GetAllUyNhiemChi()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = uynhiemchiMgt.GetAllUyNhiemChi();
            List<UyNhiemChi> listUyNhiemChi = (List<UyNhiemChi>)resultObj.Result;
            return listUyNhiemChi;
        }
    }
}
